﻿using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Mvc;

namespace BlzOidc.Controllers
{
    public class AccountController : Controller
    {
        [HttpGet]
        public async Task<IActionResult> Login(string returnUrl)
        {
            if (string.IsNullOrEmpty(returnUrl))
                returnUrl = "/";

            //if (User?.Identity?.IsAuthenticated == true)
            //{
            //    // delete local authentication cookie
            //    await HttpContext.SignOutAsync("cookies");
            //    await HttpContext.SignOutAsync("oidc");
            //}

            // start challenge and roundtrip the return URL and scheme 
            var authProps = new AuthenticationProperties
            {
                //持久化登录状态，保存在cookies
                //IsPersistent = true,
                //IsPersistent = false,
                RedirectUri = returnUrl
            };

            //发起oidc认证，跳转到Identity Server登录
            return Challenge(authProps, "oidc");
        }

        [HttpGet]
        public async Task<IActionResult> Logout()
        {
            if (User?.Identity?.IsAuthenticated == true)
            {
                // delete local authentication cookie
                await HttpContext.SignOutAsync("cookies");
            }

            var authProps = new AuthenticationProperties
            {
                RedirectUri = "/"
            };

            //跳转到Identity Server退出登录
            return SignOut(authProps, "oidc");
        }
    }
}
